var config = require("config");
var dialogManager = require("dialogManager");
cc.Class({
    extends: cc.Component,

    properties: {
        char : {
            default : null,
            type : cc.Node
        },
        char_txt : {
            default : null,
            type : cc.Node
        },
        hit_cell : {
            default : null,
            type : cc.Node
        },
        hp_cell : {
            default : null,
            type : cc.Node
        },
        locked_Nodes : {
            default : [],
            type : [cc.Node]
        },
        gold:cc.Label,
        bg : {
            default : null,
            type : cc.Node
        },
    },
    onLoad () {
        var height = cc.view.getVisibleSize().height;
        this.node.height = height;
        config.loadImage(this.bg,"pet_bg","resources/image/");
        this.bg.height = height/2*1.3;
        // console.log(">>>>>>>>>>height",this.bg.height);

        this.gold.string = config.PlayerGold;
        for(var i=1;i<=5;i++){
           if(!config.heroInfo["hero"+i].locked){
            this.locked_Nodes[i-1].active = false;
           }
        }

        //升级攻击力
        this.txt_hitLv = this.hit_cell.getChildByName('nm_atk').getChildByName('LabelAtlas_lv').getComponent(cc.Label);
        this.txt_hit = this.hit_cell.getChildByName('n_atk').getChildByName('LabelAtlas_green').getComponent(cc.Label);
        this.txt_hitHuafei = this.hit_cell.getChildByName('huafei').getChildByName('LabelAtlas_prop').getComponent(cc.Label);
        //升级血量
        this.txt_hpLv = this.hp_cell.getChildByName('nm_hp').getChildByName('LabelAtlas_lv').getComponent(cc.Label);
        this.txt_hp = this.hp_cell.getChildByName('n_atk').getChildByName('LabelAtlas_green').getComponent(cc.Label);
        this.txt_hpHuafei = this.hp_cell.getChildByName('huafei').getChildByName('LabelAtlas_prop').getComponent(cc.Label);

        this.curIndex = config.CurHeroIndex;

        if(config.heroInfo["hero"+config.CurHeroIndex].locked){
            this.curIndex = 1;
        }

        this.hitLv_num = config.heroInfo["hero"+this.curIndex].hit;
        this.hpLv_num = config.heroInfo["hero"+this.curIndex].hp;
        this.showHitLv(this.hitLv_num,false);
        this.showHpLv(this.hpLv_num,false);
        
        // if(this.curIndex != 1){
            config.loadImage(this.char,"char_"+this.curIndex,"resources/image/");
            config.loadImage(this.char_txt,"char_txt_"+this.curIndex,"resources/image/");
        // }
    },
    setGoldString(){
        this.gold.string = this.goldNum;
    },
    showChar(index){
        var self = this;
        console.log("showChar:",index)
        if(this.locked_Nodes[index-1].active == true){
            console.log(">>>>>>未解锁:",config.PlayerGold);
            var enterCall = function(){ 
                if(config.PlayerGold < config.heroInfo["hero"+index].monney){
                    console.log(">>>>>>金币不够",config.heroInfo["hero"+index].monney);
                    if(typeof(wx)!="undefined"){
                        dialogManager.showNotEnoughGoldDialog();
                    }
                }else{
                    self.locked_Nodes[index-1].active = false;
                    config.heroInfo["hero"+index].locked = false;
                    cc.vv.gameDataMgr.saveHeroInfo(index);
                    config.PlayerGold = config.PlayerGold - config.heroInfo["hero"+index].monney;
                    cc.vv.gameDataMgr.saveGold(config.PlayerGold);
                    self.gold.string = config.PlayerGold;
                }
            }
            var args = {
                title:"温馨提示",
                content:"解锁需要花费金币"+config.heroInfo["hero"+index].monney,
                enterCallback:enterCall,
                cancelCallback:null,
            }
            dialogManager.showCommonDialog(args);
            return;
        }else{
            console.log(">>>>>>已解锁")
        }

        if(this.curIndex == index)
            return;
        config.loadImage(this.char,"char_"+index,"resources/image/");
        config.loadImage(this.char_txt,"char_txt_"+index,"resources/image/");
        this.curIndex = index;
        this.hitLv_num = config.heroInfo["hero"+this.curIndex].hit;
        this.hpLv_num = config.heroInfo["hero"+this.curIndex].hp;
        this.showHitLv(this.hitLv_num);
        this.showHpLv(this.hpLv_num);
    },
    showHitLv(hitLv_num,isUp){
        if(config.PlayerGold>=(hitLv_num-1)*100*2 || !isUp){
        }else{
            console.log("金币不足");
            if(typeof(wx)=="undefined"){
                var args = {
                    title:"温馨提示",
                    content:"金币不足！",
                    enterCallback:null,
                    cancelCallback:null,
                }
                dialogManager.showCommonDialog(args);
            }else{
                dialogManager.showNotEnoughGoldDialog();
            }
            return;
        }
        this.txt_hitLv.string = hitLv_num;
        var hitnum = hitLv_num*100;
        this.txt_hit.string = hitnum;
        this.txt_hitHuafei.string = hitnum*2;
        config.heroInfo["hero"+this.curIndex].hit = hitLv_num;
        cc.vv.gameDataMgr.saveHeroInfo(this.curIndex);
        if(isUp){
            config.PlayerGold = config.PlayerGold - (hitLv_num-1)*100*2;
            cc.vv.gameDataMgr.saveGold(config.PlayerGold);
            this.gold.string = config.PlayerGold;
        }
    },
    showHpLv(hpLv_num,isUp){
        if(config.PlayerGold>=(hpLv_num-1)*200*2 || !isUp){
        }else{
            console.log("金币不足");
            if(typeof(wx)=="undefined"){
                var args = {
                    title:"温馨提示",
                    content:"金币不足！",
                    enterCallback:null,
                    cancelCallback:null,
                }
                dialogManager.showCommonDialog(args);
            }else{
                dialogManager.showNotEnoughGoldDialog();
            }
            return;
        }
        this.txt_hpLv.string = hpLv_num;
        var hpValue = hpLv_num*200;
        this.txt_hp.string = hpValue;
        this.txt_hpHuafei.string = hpValue*2;
        config.heroInfo["hero"+this.curIndex].hp = hpLv_num;
        cc.vv.gameDataMgr.saveHeroInfo(this.curIndex);
        if(isUp){
            config.PlayerGold = config.PlayerGold - (hpLv_num-1)*200*2;
            cc.vv.gameDataMgr.saveGold(config.PlayerGold);
            this.gold.string = config.PlayerGold;
        }
    },
    show(){},
    closeClick(){
        cc.vv.audioMgr.playSFX("ui_button");
        this.node.destroy();
    },
    headClick(event){
        cc.vv.audioMgr.playSFX("ui_button");
        var _name = event.target._name;
        var index = _name.replace("char_jia_", "");
        this.showChar(index);
    },
    goFightClick(){
        cc.vv.audioMgr.playSFX("ui_button");
        config.CurHeroIndex = this.curIndex;
        config.HeroMaxHp = config.heroInfo["hero"+this.curIndex].hp*100;
        config.BulletHpDrop = config.heroInfo["hero"+this.curIndex].hit*200;

        cc.director.preloadScene("gameScene", function () {
            cc.director.loadScene("gameScene");
        });
    },
    hitLvClick(){
        cc.vv.audioMgr.playSFX("ui_button");
        this.hitLv_num++;
        this.showHitLv(this.hitLv_num,true);
    },
    hpLvClick(){
        cc.vv.audioMgr.playSFX("ui_button");
        this.hpLv_num++;
        this.showHpLv(this.hpLv_num,true);
    },
});
